﻿#include "Bullet.h"
//#include "Global.h"
#include "Resource.h"

Bullet::Bullet(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{
	this->SetID(ID);
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Bullet.png"), 150, 0);

	_sprite->SetFrame(0,4);	//frame an

	_xOld = _x;
	_yOld = _y;

	if (isLeft == true)
	{
		_vx = - 0.9;
	} 
	else
	{
		_vx =  0.9;
	}

	_vy = 0;
	_accelY = 0;

	 
	container = NULL;
	 

	object_State = IS_MOVING;

	timeExplosion = 0;
}

Bullet::~Bullet()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}

void Bullet::Render()
{
	if (object_State != IS_REMOVE)
	{
		_sprite->Render(_x, _y);
	}

}

OBJECT_TYPE Bullet::GetTypeObject()
{

	return OBJECT_ITEM;
}

void Bullet::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
}

void Bullet::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	switch(object_State)
	{
	case IS_IDLE:
		//CheckObjects(staObjs,dynObjs);
		break;

	case IS_ACTIVING:
		_x+=_vx*gameTime;
		_y+=_vy*gameTime+1/2*_accelY*gameTime*gameTime;
		if(_y< this->GetBond().bottom - 32-5)
			ChangeState(IS_MOVING);
		break;

	case IS_MOVING:
		Move(gameTime,staObjs,dynObjs);
		break;

	case IS_EXPLOSION:
		timeExplosion += gameTime;
		if (timeExplosion > 200)
		{
			timeExplosion = 0;
			ChangeState(IS_REMOVE);
		}

		break;
	}

	_sprite->Update(gameTime);
	ResetRect();
}



void Bullet::Move(float TPF,list<Object*>* staObjs,list<Object*>* dynObjs)
{
	Object* obj;
	//Theo phương x
	_x+=_vx*TPF;
	ResetRect();
	list<Object*>::iterator i;
	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;
		//if(obj->GetID()>=20&&obj->GetID()<=29) //Nếu là thùng, đá , gạch...
		if (obj->GetTypeObject() == OBJECT_LAND)
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond()) == true)	
			{
				//_vx=-_vx;
				_vx = 0;
				_x = _xOld;
				ChangeState(IS_EXPLOSION);
			}
	}
	//Theo phương y
	_accelY = GRAVITY;
	_vy+=_accelY*TPF;
	_y+=_vy*TPF+1.0/2*_accelY*TPF*TPF;
	ResetRect();
	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;

		if (obj->GetTypeObject() ==OBJECT_LAND)
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
			{
				_accelY=0;
				_vy=-0.8;
				_y=_yOld;
			}
	}

	for(i=staObjs->begin();i!=staObjs->end();i++)
	{
		obj=*i;
		if (obj->GetID() == 42)			//pirhana
			if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
			{
				if (object_State != IS_REMOVE || object_State != IS_IDLE)
				{
					obj->ChangeState(IS_REMOVE);
					this->ChangeState(IS_EXPLOSION);
				}
			}
	}
	for(i=dynObjs->begin();i!=dynObjs->end();i++)
	{
		obj=*i;
		if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
		{
			switch(obj->GetID())
			{
			case 11:
				if (object_State == ES_ACTIVING)
				{
					obj->ChangeState(ES_FALL);
					this->ChangeState(IS_EXPLOSION);
				}
				break;
			case 12:
				if (object_State == ES_ACTIVING || object_State == ES_STOP)
				{
					obj->ChangeState(ES_FALLBULLET);
					this->ChangeState(IS_EXPLOSION);
				}
				break;
			}
		}
	}

	_xOld=_x;
	_yOld=_y;
}

void Bullet::ChangeState(int state)
{
	object_State = state;
	switch(state)
	{
	case IS_ACTIVING:
		_vy=-4.00;
		break;

	case IS_MOVING:
		_vy=0;
		_vx=0.200;
		break;

	case IS_EXPLOSION:
		_sprite->SetFrame(5,8);
		break;

	case IS_REMOVE:
		break;
	}
}